package gameEngine.agents.Logger;

import exportSystem.statistics.Statistics;

/**
 * CSV file logger
 * @author alon
 *
 */
public class CSVLogger extends textLogger {
	double score;
	
	/**
	 * uses {@link textLogger}constructor to create a csv file
	 * @param gameNum game ID
	 */
	public CSVLogger(int gameNum){
		super("csv",gameNum);
	}
	
	/**
	 * writes remaining text to file and close stream
	 */
	public void flush(){
		counter=0;
		try{
			// write remaining massages
			out.write(str);
			//write the passing table
			out.write(formatPassTable());
			//write the total score
			out.write("\n game total score was  "+score);
			//Close the output stream
			out.close();
			log.debug("done writing a buffer");
		}catch (Exception e){//Catch exception if any
			log.debug("writing log failed\n"+ e.getMessage());
		}
		str="";
	}
	
	/**
	 * get passing table from {@link Statistics} and add it to CSV
	 * @return
	 */
	private String formatPassTable(){
		int[][] passTable=Statistics.getPassTable();
		int size=passTable.length;
		//header
		String result="passing table:\n";
		for(int i=1;i<size+1;i++){
			//column index ,1,2,3,....
			result+=","+i;
		}
		result+="\n";
		for(int i=0;i<size;i++){
			result+=(i+1)+",";//row index
			for(int j=0;j<size-1;j++){
				//row value
				result+=passTable[i][j]+",";
			}
			//last in row
			result+=passTable[i][size-1]+"\n";
		}
		return result;	
	}
	
	public void setScore(double score){
		this.score=score;
	}
}
